#include "sdk.h"
#include "externaldepsdlg.h"
#include <wx/msgdlg.h>
#include "editpathdlg.h"

BEGIN_EVENT_TABLE( ExternalDepsDlg, wxScrollingDialog )
  EVT_UPDATE_UI( -1, ExternalDepsDlg::OnUpdateUI )
  EVT_LISTBOX_DCLICK( XRCID( "lstAdditionalFiles" ), ExternalDepsDlg::OnEditAdditional )
  EVT_LISTBOX_DCLICK( XRCID( "lstExternalFiles" ), ExternalDepsDlg::OnEditExternal )
  EVT_BUTTON( XRCID( "btnAddAdditional" ), ExternalDepsDlg::OnAddAdditional )
  EVT_BUTTON( XRCID( "btnEditAdditional" ), ExternalDepsDlg::OnEditAdditional )
  EVT_BUTTON( XRCID( "btnDelAdditional" ), ExternalDepsDlg::OnDelAdditional )
  EVT_BUTTON( XRCID( "btnAddExternal" ), ExternalDepsDlg::OnAddExternal )
  EVT_BUTTON( XRCID( "btnEditExternal" ), ExternalDepsDlg::OnEditExternal )
  EVT_BUTTON( XRCID( "btnDelExternal" ), ExternalDepsDlg::OnDelExternal )
END_EVENT_TABLE()

ExternalDepsDlg::ExternalDepsDlg( wxWindow* parent, M_Code_Project* project, ProjectBuildTarget* target ) : m_pProject( project ), m_pTarget( target ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/external_deps.xrc" ) ;
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgExternalDeps" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_CANCEL", wxButton )->SetDefault();
  FillAdditional();
  FillExternal();
}

ExternalDepsDlg::~ExternalDepsDlg() {
}

void ExternalDepsDlg::FillAdditional() {
  wxListBox* lst = XRCCTRL( *this, "lstAdditionalFiles", wxListBox );
  lst->Clear();
  wxArrayString array = GetArrayFromString( m_pTarget->GetAdditionalOutputFiles() );
  for( unsigned int i = 0; i < array.GetCount(); ++i ) {
    lst->Append( array[i] );
  }
}

void ExternalDepsDlg::FillExternal() {
  wxListBox* lst = XRCCTRL( *this, "lstExternalFiles", wxListBox );
  lst->Clear();
  wxArrayString array = GetArrayFromString( m_pTarget->GetExternalDeps() );
  for( unsigned int i = 0; i < array.GetCount(); ++i ) {
    lst->Append( array[i] );
  }
}

void ExternalDepsDlg::EndModal( int retCode ) {
  wxString deps;
  wxListBox* lst = XRCCTRL( *this, "lstExternalFiles", wxListBox );
  for( unsigned int i = 0; i < ( unsigned int )lst->GetCount(); ++i ) {
    deps << lst->GetString( i ) << _T( ';' );
  }
  m_pTarget->SetExternalDeps( deps );
  wxString files;
  lst = XRCCTRL( *this, "lstAdditionalFiles", wxListBox );
  for( unsigned int i = 0; i < ( unsigned int )lst->GetCount(); ++i ) {
    files << lst->GetString( i ) << _T( ';' );
  }
  m_pTarget->SetAdditionalOutputFiles( files );
  return wxScrollingDialog::EndModal( retCode );
}

#define DoAdd(listbox,message)                              \
  {                                                           \
    wxListBox* lst = XRCCTRL(*this, listbox, wxListBox);    \
    EditPathDlg dlg(this,                                   \
                    m_pProject->GetBasePath(),              \
                    m_pProject->GetBasePath(),              \
                    message,                                \
                    wxEmptyString,                          \
                    false);                                 \
    PlaceWindow(&dlg);                                      \
    if (dlg.ShowModal() == wxID_OK)                         \
      lst->Append(dlg.GetPath());                         \
  }

#define DoEdit(listbox,message)                             \
  {                                                           \
    wxListBox* lst = XRCCTRL(*this, listbox, wxListBox);    \
    int sel = lst->GetSelection();                          \
    if (sel == -1)                                          \
      return;                                             \
    EditPathDlg dlg(this,                                   \
                    lst->GetStringSelection(),              \
                    m_pProject->GetBasePath(),              \
                    message,                                \
                    wxEmptyString,                          \
                    false);                                 \
    PlaceWindow(&dlg);                                      \
    if (dlg.ShowModal() == wxID_OK)                         \
      lst->SetString(sel, dlg.GetPath());                 \
  }

#define DoDel(listbox)                                      \
  {                                                           \
    wxListBox* lst = XRCCTRL(*this, listbox, wxListBox);    \
    int sel = lst->GetSelection();                          \
    if (sel == -1)                                          \
      return;                                             \
    if (msging(_T("Are you sure you want to remove this file?"), _T("Remove file"), wxYES_NO | wxNO_DEFAULT, this) == wxID_NO)   \
      return;                                             \
    lst->Delete(sel);                                       \
  }

void ExternalDepsDlg::OnAddAdditional( M_Code_unused wxCommandEvent& event ) {
  DoAdd( "lstAdditionalFiles", _T( "Add additional output file" ) );
}

void ExternalDepsDlg::OnEditAdditional( M_Code_unused wxCommandEvent& event ) {
  DoEdit( "lstAdditionalFiles", _T( "Edit additional output file" ) );
}

void ExternalDepsDlg::OnDelAdditional( M_Code_unused wxCommandEvent& event ) {
  DoDel( "lstAdditionalFiles" );
}

void ExternalDepsDlg::OnAddExternal( M_Code_unused wxCommandEvent& event ) {
  DoAdd( "lstExternalFiles", _T( "Add external dependency file" ) );
}

void ExternalDepsDlg::OnEditExternal( M_Code_unused wxCommandEvent& event ) {
  DoEdit( "lstExternalFiles", _T( "Edit external dependency file" ) );
}

void ExternalDepsDlg::OnDelExternal( M_Code_unused wxCommandEvent& event ) {
  DoDel( "lstExternalFiles" );
}

void ExternalDepsDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  int selAdd = XRCCTRL( *this, "lstAdditionalFiles", wxListBox )->GetSelection();
  int selExt = XRCCTRL( *this, "lstExternalFiles", wxListBox )->GetSelection();
  XRCCTRL( *this, "btnEditAdditional", wxButton )->Enable( selAdd != -1 );
  XRCCTRL( *this, "btnDelAdditional", wxButton )->Enable( selAdd != -1 );
  XRCCTRL( *this, "btnEditExternal", wxButton )->Enable( selExt != -1 );
  XRCCTRL( *this, "btnDelExternal", wxButton )->Enable( selExt != -1 );
}
